#ifndef __TMERE_H__
#define __TMERE_H__

#include <stdio.h>
#include <msgQLib.h>
#include <semLib.h>
#include <taskLib.h>
#include <wdLib.h>
#include <stdint.h>
#include "tJournal.h"
#include "comm.h"
#include "tRempliCarton.h"
#include "tEnvoi.h"
#include "tErreur.h"
#include "tPalette.h"
#include "tReception.h"
#include "tEntrepot.h"
#include "tExpedition.h"

#define TYPE1 1
#define TYPE2 2

#define PIECE_DEFECTUEUSE 0
#define PIECE_OK 1

#define ERROR_INIT 10
#define ERROR_START 20

/*************************TYPE DEFINITIONS ******************************/ 
/* This structure allows to store the number of pieces of type 1 and type 2 in the
 Warehouse*/
typedef struct{
    int16_t nbPType1; 
    int16_t nbPType2;
} stock_entrepot;

/************************ PUBLIC VARIABLES  ******************************/

/* Table containing the IDs of the tasks */
int16_t tabTaches[7];

/* Message queue IDs */
MSG_Q_ID msg_error; // message queue used to stock errors
MSG_Q_ID msg_log; // message queue used to log all transactions
MSG_Q_ID msg_extinction; // message queue used to stop the application

/* Data 'nbCar' defines the number of Boxes in the queue of boxes wainting
 *  to be packaged in the Palette*/
int16_t nbCar;
/* Data 'nbPal' defines the number of Palettes in the queue of palettes 
 * wainting to be stored in the Warehouse*/
int16_t nbPal;
/* Data 'nbPiec' defines the number of Pieces in the queue of pieces 
 * wainting to be packaged in the Box*/
int16_t nbPiec;

/* Semaphore IDs */
SEM_ID semStock;
SEM_ID semCart;
SEM_ID semPalette;
SEM_ID semClapet;

/* Timer's ID used to alarm the lack of Pieces */
WDOG_ID alarmeId;

/* Data 'stock' defines the Inventory */
stock_entrepot stock;

/* Data 'piece' defines a Piece and indicates whether a Piece is Ok (value 1)
 * or is Defective (valeu 0) */
int16_t piece;

/* Data 'clapet' defines the opening or the closing of the valve
 * through the Pieces arrive in.
 * It equals 1 in case of Error, it means that the valve is closed*/ 
int8_t clapet;


/* Data send by the operator */
int16_t nbPalettes; // number of palletes a lot should contain
int16_t nbCartons; // number of boxes a palette should contain
int16_t nbPieces; // number of pieces a carton should contain
int16_t numLot; // number of the lot
int16_t nbPiecesDef; // max number of defective pieces allowed

/* Data 'typePiece' defines the type of pieces of the lot*/
int16_t typePiece; 

/* Data 'presPal' defines the value of the sensor verifying the presence of a palette.
 * Data 'presCart' defines the value of the sensor verifying the presence of a box.
 * They value 0 whether the palette (or the carton) is not present 
 * and 1 whether the palette is present.
 * his values are defined in TSimulation*/
int8_t presPal;
int8_t presCart;

 /* Data 'etatImprimante' defines the state of the printer.
  * It equals 1 if the printer is Ok and 0 if it is breaked.
  * The values are defined in TSimulation*/ 
int16_t etatImprimante;


/************************ PUBLIC FUNCTIONS ******************************/
int16_t tMere ();

#endif
